package org.project.single.controller.back;

import cn.hutool.core.util.ObjectUtil;
import lombok.Getter;
import org.project.single.pojo.constant.SysConst;
import org.project.single.pojo.ro.EnableRo;
import org.project.single.pojo.ro.menu.EditMenuBackRo;
import org.project.single.pojo.ro.menu.MenuListBackRo;
import org.project.single.pojo.vo.menu.MenuDetailBackVo;
import org.project.single.pojo.vo.menu.MenuListBackVo;
import org.project.single.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.stone.project.baseCommon.pojo.constant.SysPropConstant;
import org.stone.project.baseCommon.pojo.vo.R;

import javax.validation.Valid;
import java.util.List;

/**
 * 菜单后台接口
 */
@RestController
@RequestMapping("/back/menu")
public class SysMenuBackController {
    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 获取菜单列表
     * @return
     */
    @GetMapping("getMenuList")
    public R<List<MenuListBackVo>> getMenuList(MenuListBackRo menuListBackRo){
        return R.ok(sysMenuService.getMenuListBack(menuListBackRo));
    }

    /**
     * 菜单详情
     * @param id
     * @return
     */
    @GetMapping("detail/{id}")
    public R<MenuDetailBackVo> getDetail(@PathVariable String id){
        return R.ok(sysMenuService.getDetailBack(id));
    }



    /**
     * 添加菜单
     * @return
     */
    @PostMapping("addMenu")
    public R addMenu(@RequestBody @Validated EditMenuBackRo editMenuBackRo){
        if (ObjectUtil.isNull(editMenuBackRo.getParentCode())){
            editMenuBackRo.setParentCode(SysPropConstant.EMPTY_STR);
        }
        sysMenuService.editMenuBack(editMenuBackRo);
        return R.ok();
    }

    /**
     * 编辑菜单
     * @param editMenuBackRo
     * @return
     */
    @PutMapping("updateMenu")
    public R updateMenu(@RequestBody @Validated EditMenuBackRo editMenuBackRo){
        editMenuBackRo.setChannel(null);
        sysMenuService.editMenuBack(editMenuBackRo);
        return R.ok();
    }

    /**
     * 菜单删除
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public R deleteMenu(@PathVariable String id){
        sysMenuService.deleteMenuBack(id);
        return R.ok();
    }

    /**
     * 菜单启用
     * @return
     */
    @PostMapping("enableMenu")
    public R enableMenu(@RequestBody @Valid EnableRo enableRo){
        sysMenuService.enableMenu(enableRo);
        return R.ok();
    }
}
